﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.ServiceProcess;
using System.Text;
using System.Threading;
using System.IO;

namespace CassiniService
{
    public partial class CassiniServer : ServiceBase
    {
        public static Cassini.Server server = null;
        Mutex mutexStart = null;
        Mutex mutextRuning = null;
        static string _appPath;
        static int _portNumber;
        static string _virtRoot;
        public CassiniServer()
        {
            InitializeComponent();
        }
        private void Log(string message)
        {
            string fileName = Path.Combine(@"D:\mywork\private\cassini-runner\", "log.txt");
            if (message != null)
            {
                File.AppendAllText(fileName, message);
            }
        }
        private void SetMutex()
        {
            
            mutextRuning = new Mutex(false, "mycassini_running" + _portNumber);
        }
        public void StartService(string[] args)
        {
            OnStart(args);
        }
        private static void StartService()
        {

        }
        protected override void OnStart(string[] args)
        {
            _portNumber = 89;
            _virtRoot = "/";
            _appPath = @"D:\mywork\private\cassini-runner\output";
            try
            {
                mutexStart = new Mutex(false, "mycassini" + _portNumber);
                if (mutexStart.WaitOne())
                {
                    Log($"\r\n开始启动服务器：\r\nPath:{_appPath},Port:{_portNumber}");
                    server = new Cassini.Server(_portNumber, _virtRoot, _appPath);
                    server.Start();
                    Log($"\r\n服务器已启动：\r\nPath:{_appPath},Port:{_portNumber}");
                }
            }
            catch (Exception ex)
            {
                mutexStart?.ReleaseMutex();
                server?.Stop();
            }
        }
        public void StopService()
        {
            this.OnStop();
        }
        protected override void OnStop()
        {
            Log($"\r\n禁止服务器重复启动.\r\nPath{_appPath},Port:{_portNumber}");
            try
            {
                if (mutexStart.WaitOne())
                {
                    return;
                }
            }
            catch(Exception ex)
            {
                Log($"\r\n防止重复启动出错：{ex.Message}");
            }
            if (server != null)
            {
                try
                {
                    Log($"\r\n开始关闭服务器.\r\nPath{_appPath},Port:{_portNumber}");
                    server.Stop();
                    Log($"\r\n服务器已关闭：\r\nPath{_appPath},Port:{_portNumber}");
                }
                catch(Exception ex)
                {
                    Log($"\r\n关闭服务器出错：{ex.Message}");
                }
            }
        }
        private static void ParseArgs(String[] args)
        {
            string portString = null;
            try
            {
                if (args.Length >= 1)
                {
                    _appPath = args[0].Trim();
                    if (_appPath[0] == '/')
                    {
                        _appPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, _appPath.Substring(1));
                    }
                }

                if (args.Length >= 2)
                    portString = args[1];

                if (args.Length >= 3)
                    _virtRoot = args[2];
            }
            catch
            {
            }

            if (portString == null)
                portString = "89";

            _portNumber = Int32.Parse(portString);
            if (_virtRoot == null)
                _virtRoot = "/";

            if (_appPath == null)
                _appPath = _appPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "WebFormApp");
        }
    }
}
